package com.ruoyi.erp.safetyinstruction.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erp.safetyinstruction.domain.SafetyInstructionTab;
import com.ruoyi.erp.safetyinstruction.service.SafetyInstructionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 2017.1.1
 * @Auther Du Ruifang @萌えキュンキュン
 * @Date 2020/12/25 10:11
 * @description 该类的作用
 */
@Controller
@RequestMapping("/erp/safety")
public class SafetyInstructionController extends BaseController {
    @Autowired
    private SafetyInstructionService safetyInstructionService;


    /**
     * 查询安全代码列表
     * @param safetyInstructionTab
     * @return
     */
    @PreAuthorize("@ss.hasPermi('erp:safety:list')")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo getList (SafetyInstructionTab safetyInstructionTab) {
        startPage();
        List<SafetyInstructionTab> safetyInstructionTabList = safetyInstructionService.getList(safetyInstructionTab);
        return getDataTable(safetyInstructionTabList);
    }


    /**
     * 添加安全代码数据
     * @param safetyInstructionTab
     * @return
     */
    @PreAuthorize("@ss.hasPermi('erp:safety:add')")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult saveSafetyInstruction (@RequestBody  SafetyInstructionTab safetyInstructionTab){
        try {
            safetyInstructionService.saveSafetyInstruction(safetyInstructionTab);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("添加失败");
        }
        return AjaxResult.success("添加成功");
    }

    /**
     * 根据hazardCode查询安全代码详情
     * @param  hazardCode
     * @return
     */
    @PreAuthorize("@ss.hasPermi('erp:safety:query')")
    @GetMapping("/getInfoByHazardCode/{hazardCode}")
    @ResponseBody
    public AjaxResult getInfoByHazardCode (@PathVariable String hazardCode) {
      return AjaxResult.success(safetyInstructionService.getInfoByHazardCode(hazardCode));
    }

    /**
     * 修改保存安全代码
     */
    @PreAuthorize("@ss.hasPermi('erp:safety:edit')")
    @PutMapping("/edit")
    @ResponseBody
    public AjaxResult editSaveSafetyInstruction(@RequestBody  SafetyInstructionTab safetyInstructionTab){
        try {
            safetyInstructionService.editSaveSafetyInstruction(safetyInstructionTab);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return AjaxResult.error("修改保存失败");
        }
        return AjaxResult.success("修改保存成功");
    }

    /**
     * 删除安全代码和批量删除
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('erp:safety:remove')")
    @DeleteMapping("/remove/{ids}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("ids") String[] ids) {
        return toAjax(safetyInstructionService.deleteSafetyInstruction(ids));
    }

    /**
     * 导出安全代码文档
     * @param safetyInstructionTab
     * @return
     */
    @PreAuthorize("@ss.hasPermi('erp:safety:export')")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SafetyInstructionTab safetyInstructionTab)
    {
        List<SafetyInstructionTab> safetyInstructionTabList = safetyInstructionService.getList(safetyInstructionTab);
        ExcelUtil<SafetyInstructionTab> util = new ExcelUtil<SafetyInstructionTab>(SafetyInstructionTab.class);
        return util.exportExcel(safetyInstructionTabList, "交货路线数据");
    }
}
